package com.roc.steelend.controller;


import com.auth0.jwt.interfaces.DecodedJWT;
import com.roc.steelend.controller.vo.reconciliation.FindReconciliationReception;
import com.roc.steelend.controller.vo.returnformat.ReturnMsg;
import com.roc.steelend.controller.vo.returnformat.ReturnMsgForTable;
import com.roc.steelend.controller.vo.tiaopiao.FindTiaoPiaoReception;
import com.roc.steelend.controller.vo.tiaopiao.TiaoPiaoBuyReception;
import com.roc.steelend.controller.vo.tiaopiao.TiaoPiaoKaipingReception;
import com.roc.steelend.controller.vo.tiaopiao.TiaoPiaoSaleReception;
import com.roc.steelend.pojo.TiaoPiaoBuy;
import com.roc.steelend.pojo.TiaoPiaoKaiping;
import com.roc.steelend.pojo.TiaoPiaoSale;
import com.roc.steelend.service.TiaoPiaoBuyService;
import com.roc.steelend.service.TiaoPiaoKaipingService;
import com.roc.steelend.service.TiaoPiaoSaleService;
import com.roc.steelend.util.JWTUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.math.BigInteger;
import java.util.List;

@RestController
@CrossOrigin
@Api(tags = "调票功能接口")
public class TiaoPiaoController {

    @Autowired
    private TiaoPiaoBuyService tiaoPiaoBuyService;
    @Autowired
    private TiaoPiaoSaleService tiaoPiaoSaleService;
    @Autowired
    private TiaoPiaoKaipingService tiaoPiaoKaipingService;

    @PostMapping("/v1/post/tiaopiao/buy")
    public ReturnMsg postTiaoPiaoBuy(@RequestBody TiaoPiaoBuyReception tiaoPiaoBuyReception){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

        System.out.println(tiaoPiaoBuyReception);

        String token = request.getHeader("token");
        DecodedJWT decodedJWT = JWTUtil.getToken(token);
        String employeeIdAsString = decodedJWT.getClaim("employeeId").asString();
        return tiaoPiaoBuyService.postTiaoPiaoBuy(tiaoPiaoBuyReception,Integer.valueOf(employeeIdAsString));

    }


    @PostMapping("/v1/post/tiaopiao/sale")
    public ReturnMsg postTiaoPiaoSale(@RequestBody TiaoPiaoSaleReception tiaoPiaoSaleReception){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

        String token = request.getHeader("token");
        DecodedJWT decodedJWT = JWTUtil.getToken(token);
        String employeeIdAsString = decodedJWT.getClaim("employeeId").asString();
        return tiaoPiaoSaleService.postTiaoPiaoBySale(tiaoPiaoSaleReception,Integer.valueOf(employeeIdAsString)) ;

    }


    @PostMapping("/v1/post/tiaopiao/kaiping")
    public ReturnMsg postTiaoPiaoKaiping(@RequestBody TiaoPiaoKaipingReception tiaoPiaoKaipingReception){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();



        String token = request.getHeader("token");
        DecodedJWT decodedJWT = JWTUtil.getToken(token);
        String employeeIdAsString = decodedJWT.getClaim("employeeId").asString();
        return tiaoPiaoKaipingService.postTiaoPiaoByKaiping(tiaoPiaoKaipingReception,Integer.valueOf(employeeIdAsString)) ;

    }



    @PostMapping("/v1/find/tiaopiao/data")
    @ApiOperation(value = "调票查询接口(ps:特殊情况使用post,但不修改数据!!!)", notes = "调票查询接口(ps:特殊情况使用post,但不修改数据!!!)")
    public ReturnMsgForTable findTiaoPiaoData(@RequestBody FindTiaoPiaoReception tiaoPiaoReception) throws InterruptedException {

        ReturnMsgForTable returnMsgForTable = new ReturnMsgForTable();


        if (tiaoPiaoReception.getTiaopiaoType()==1){
//采购
            returnMsgForTable.setCurrenPage(tiaoPiaoReception.getCurrentPage());
            returnMsgForTable.setObjectList(tiaoPiaoBuyService.findTiaoPiaoDataByBuy(tiaoPiaoReception));
            returnMsgForTable.setTotal(tiaoPiaoBuyService.findTiaoPiaoDataByBuyCount(tiaoPiaoReception));
        }else if(tiaoPiaoReception.getTiaopiaoType()==2){
//销售
            returnMsgForTable.setCurrenPage(tiaoPiaoReception.getCurrentPage());
            returnMsgForTable.setObjectList(tiaoPiaoSaleService.findTiaoPiaoDataBySale(tiaoPiaoReception));
            returnMsgForTable.setTotal(tiaoPiaoSaleService.findTiaoPiaoDataBySaleCount(tiaoPiaoReception));
        }else if (tiaoPiaoReception.getTiaopiaoType()==3){
//开平
            returnMsgForTable.setCurrenPage(tiaoPiaoReception.getCurrentPage());
            returnMsgForTable.setObjectList(tiaoPiaoKaipingService.findTiaoPiaoDataByKaiping(tiaoPiaoReception));
            returnMsgForTable.setTotal(tiaoPiaoKaipingService.findTiaoPiaoDataByKaipingCount(tiaoPiaoReception));
        }

        return  returnMsgForTable;
    }


    /**
     * 采购调票细节查询
     */
    @GetMapping("/v1/buy/tiaoPiao/detail/select")
    public List<TiaoPiaoBuy> buyTiaoPiaoDetailSelect(@RequestParam("tiaoPiaoId") BigInteger tiaoPiaoId){

        return tiaoPiaoBuyService.buyTiaoPiaoDetailSelect(tiaoPiaoId);
    }

    /**
     * 销售调票细节查询
     */
    @GetMapping("/v1/sale/tiaoPiao/detail/select")
    public List<TiaoPiaoSale> saleTiaoPiaoDetailSelect(@RequestParam("tiaoPiaoId") BigInteger tiaoPiaoId){

        return tiaoPiaoSaleService.saleTiaoPiaoDetailSelect(tiaoPiaoId);
    }


    /**
     * 开平调票细节查询
     */
    @GetMapping("/v1/kaiping/tiaoPiao/detail/select")
    public List<TiaoPiaoKaiping> kaipingTiaoPiaoDetailSelect(@RequestParam("tiaoPiaoId") BigInteger tiaoPiaoId){

        return tiaoPiaoKaipingService.kaipingTiaoPiaoDetailSelect(tiaoPiaoId);
    }
}

